<?php

namespace App\Console\Commands\Plane;

use App\Facades\AirportsGeoFacade;
use App\Models\Common\Area\Repositories\Interfaces\AreaRepositoryInterface;
use App\Models\Plane\Airports\Repositories\Interfaces\AirportRepositoryInterface;
use Illuminate\Console\Command;
use Maatwebsite\Excel\Facades\Excel;

class AirportJson extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'airports:json';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成机场 json';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $airports = Excel::toArray(collect(), storage_path('plane/seeds/plane_airports.xlsx'));
        $cities   = Excel::toArray(collect(), storage_path('plane/seeds/plane_cities.xlsx'));
        unset($airports[0][0]);
        unset($cities[0][0]);
        $aps      = $airports[0];
        $cs       = $cities[0];
        $ccs      = [];
        $xj_ccs   = [];
        $areaRepo = app(AreaRepositoryInterface::class);
        foreach ($cs as $key => $c) {
            if (!$c[0] || !$c[1] || !$c[3] || !$c[4] || !$c[5] || !$c[6]) {
                $this->info("数据不全");
                var_dump($c);
                unset($cs[$key]);
                continue;
            }

            $area       = $areaRepo->findByCode($c[3]);
            $ccs[$c[0]] = [
                'city_code'    => $c[0],
                'city_name'    => $c[1],
                'city_name_uy' => $c[5],
                'adcode'       => $c[3],
                'letter_uy'    => $c[6],
                'letter'       => $area->getLetter(),
            ];
            if ($area->level == 'city') {
                if ($area->parent_code == 650000) {
                    $xj_ccs[$c[0]] = $ccs[$c[0]];
                }
            } else if ($area->level == 'district') {
                $c_area = $areaRepo->findByCode($area->parent_code);
                if ($c_area->parent_code == 650000) {
                    $xj_ccs[$c[0]] = $ccs[$c[0]];
                }
            }
        }

        $aap         = [];
        $xj_aap      = [];
        $airportRepo = app(AirportRepositoryInterface::class);
        foreach ($aps as $k => $ap) {
            if (!$ap[0]) {
                unset($aps[$k]);
                continue;
            }
            $airport = $airportRepo->findOneByName($ap[0]);
            if (!$airport) {
                continue;
            }
            AirportsGeoFacade::addGeoByAirport($airport);
            $full_name                = $ap[0];
            $full_name_uy             = $ap[2];
            $full_letter              = $ap[3];
            $short_name               = $ap[6];
            $short_name_uy            = $ap[7];
            $city_code3               = $airport->getPlaneCityCode();
            $airport_code             = $airport->getCode();
            $aap[$airport->getCode()] = [
                "code"          => $airport_code,
                "full_name"     => $full_name,
                "full_letter"   => $full_letter,
                "short_name"    => $short_name,
                "full_name_uy"  => $full_name_uy,
                "short_name_uy" => $short_name_uy,
                "city_code"     => $city_code3,
                "city_name"     => $ccs[$city_code3]['city_name'] ?? '',
                "city_name_uy"  => $ccs[$city_code3]['city_name_uy'] ?? '',
            ];

            if (isset($xj_ccs[$city_code3])) {
                $xj_aap[$airport_code] = $aap[$airport_code];
            }
        }

        $json_map = collect($ccs)->toJson(JSON_UNESCAPED_UNICODE);
        file_put_contents(public_path('Plane/cities.json'), $json_map);
        $xjc = collect($xj_ccs)->toJson(JSON_UNESCAPED_UNICODE);
        file_put_contents(public_path('Plane/xj_cities.json'), $xjc);

        $air_map = collect($aap)->toJson(JSON_UNESCAPED_UNICODE);
        file_put_contents(public_path('Plane/airports.json'), $air_map);
        $xj_ap = collect($xj_aap)->toJson(JSON_UNESCAPED_UNICODE);
        file_put_contents(public_path('Plane/xj_airports.json'), $xj_ap);
        return 0;
    }
}
